package com.bob.idea.gui;


import com.bob.common.DataStore;
import com.bob.idea.common.Constant;
import com.bob.idea.gui.action.*;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

public class MainFrame extends JFrame {
    private JLabel jLabel = new JLabel("请选择项目名称:",JLabel.LEFT);
    private JButton buttonOk = new JButton("确定");
    private JButton buttonCancel = new JButton("取消");
    private Box b1 = Box.createVerticalBox();
    private Box b2 = Box.createHorizontalBox();
    private JFileChooser fileChooser = new JFileChooser(DataStore.getDataStore().getRootDir());
    public static MainFrame mainFrame = new MainFrame();
    public static MainFrame getMainFrame(){
        return mainFrame;
    }
    private MainFrame() {
        super.setTitle(DataStore.getDataStore().getRootDir());
        super.setBounds(0, 0, 400, 800);
        super.setVisible(true);
        super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        super.add(b1);
    }
    public void init(){
        b1.setAlignmentX(0.1f);
        buttonOk.addActionListener(new ButtonOkActionListener());
        buttonCancel.addActionListener(new ButtonCancelActionListener());

        // 项目选择面板，文件选择器
        JPanel panel = new JPanel();
            panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        // 初始化项目选项列表
        Consumer<Box> initArtifactIdCheckBox  = b11 -> {
            CheckBoxActionListener checkBoxActionListener = new CheckBoxActionListener();
            DataStore.getDataStore().getPomEntityMap().forEach((artifactId,v)->{
                if(v.getPackaging()!=null &&!v.getPackaging().equals(Constant.FILETYPE_WAR)){
                    return;
                }
                JCheckBox checkBox = new JCheckBox( v.getFinalName());
                checkBox.setName(artifactId);
                checkBox.addItemListener(checkBoxActionListener);
                b11.add(checkBox);
            });
        };
        // 初始化文件按选择器
        Consumer<JFileChooser> initFileChooser = fileChooser -> {
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setApproveButtonText("选择此路径");
            fileChooser.setControlButtonsAreShown(true);
            fileChooser.setApproveButtonToolTipText("请选择文件输出路径");
            fileChooser.addActionListener(new FileChooserActionListener());
        };

        Box b11 = Box.createVerticalBox();
            b11.add(jLabel);
            initArtifactIdCheckBox.accept(b11);
        panel.add(b11);
        // 添加高度为20的横向框架
        b1.add(panel);
        b1.add(Box.createVerticalStrut(20));
             initFileChooser.accept(fileChooser);
        b1.add(fileChooser);
        b1.add(Box.createVerticalStrut(20));
        b2.add(buttonOk);
        b2.add(buttonCancel);
        b1.add(b2);

        super.show();
    }

    public void dispose() {
        super.dispose();
    }
}
